﻿using Microsoft.Data.SqlClient;
using System.Data;

namespace Net7Music_Api
{
    public class DBHelper
    {
        private static string conStr = "Data Source=120.79.8.198;Initial Catalog=Net7MusicDB;Persist Security Info=True;User ID=sa;Password=he123.";
        //private static string conStr = "Data Source=.;Initial Catalog=Net7MusicDB;Integrated Security=True";

        private static readonly SqlConnection con = new(conStr);

        public static SqlDataReader GetDataReader(string sql)
        {
            try
            {
                SqlCommand cmd = new(sql, con);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;
            }
            catch
            {
                if (con.State != ConnectionState.Closed)
                    con.Close();
                throw;
            }

        }
        public static bool ExcuteBySql(string sql)
        {
            try
            {
                SqlCommand cmd = new(sql, con);
                con.Open();
                int result = cmd.ExecuteNonQuery();
                con.Close();
                return result > 0;
            }
            catch
            {
                if (con.State != ConnectionState.Closed)
                    con.Close();
                throw;
            }

        }
    }
}
